//
//  TTNetworkTools.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/21.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON
import DaisyNet

class TTNetworkTools {
    class func getRequestData(URLString: String, jointArr : [Any]?, parameters : [String : Any]?, finishedCallback :  @escaping (_ result: JSON) -> ()) {
        var newUrl = URLString
        if jointArr != nil {
            for item in jointArr! {
                newUrl = newUrl + "/\(item)"
            }
        }
        Alamofire.request(newUrl, method: HTTPMethod.get, parameters: parameters).responseJSON { (response) in
            guard response.result.value != nil else {
                print("get失败返回：\(response)")
                return
            }
            print("get成功返回：\(response.result.value!)")
            finishedCallback(JSON(response.value!))
        }
    }
    
    class func postRequestData(URLString : String, jointArr : [Any]?, parameters : [String : Any]?, finishedCallback :  @escaping (_ result: JSON) -> ()) {
        var newUrl = URLString
        if jointArr != nil {
            for item in jointArr! {
                newUrl = newUrl + "/\(item)"
            }
        }
        // ["content-type":"application/json;charset=UTF-8"]
        Alamofire.request(newUrl, method: HTTPMethod.post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
            guard response.result.value != nil else {
                print("post失败返回：\(response)")
                return
            }
            print("post成功返回：\(response.result.value!)")
            finishedCallback(JSON(response.value!))
        }
    }
    
    class func downLoadMP3(downloadfileURL : String, finishedCallback :  @escaping (_ result: JSON) -> ()) {
        DaisyNet.download(downloadfileURL).downloadProgress { progress in
            print(progress)
            }.response { response in
                print(response)
                
        }
        
    }
}
